﻿Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports System.Data.Common

Public Class SerialData

#Region "Declaracion"
    Private _database As Database
#End Region

#Region "Constructor"
    Public Sub New()
        _database = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("Vinum")
    End Sub
#End Region

#Region "Funciones"
    Public Function ConsultarSerial(ByVal Serial As SerialObj) As SerialObj
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaConsultarSerial")
                Dim SerialRetorno As New SerialObj

                _database.AddInParameter(Comando, "Opcion", DbType.Int32, 0)
                _database.AddInParameter(Comando, "Encriptado", DbType.String, Serial.Encriptado)

                _database.AddOutParameter(Comando, "Serial", DbType.String, 100)
                _database.AddOutParameter(Comando, "Trial", DbType.Byte, 100)
                _database.AddOutParameter(Comando, "FechaIngreso", DbType.String, 100)

                _database.ExecuteNonQuery(Comando)

                SerialRetorno.Serial = Convert.ToString(_database.GetParameterValue(Comando, "Serial"))
                SerialRetorno.Trial = Convert.ToBoolean(_database.GetParameterValue(Comando, "Trial"))
                SerialRetorno.FechaIngreso = Convert.ToString(_database.GetParameterValue(Comando, "FechaIngreso"))

                Return SerialRetorno

                Transaccion.Complete()

            Catch ex As SqlException
                Throw ex
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Function

    Public Function ComputadoraYaRegistrada(ByVal Serial As SerialObj) As Boolean
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaConsultarSerial")
                _database.AddInParameter(Comando, "Opcion", DbType.Int32, 1)
                _database.AddInParameter(Comando, "Encriptado", DbType.String, Serial.Encriptado)

                _database.AddOutParameter(Comando, "CantidadDeRegistrosEncontrados", DbType.Int32, 100)

                _database.ExecuteNonQuery(Comando)

                If Convert.ToInt32(_database.GetParameterValue(Comando, "CantidadDeRegistrosEncontrados")) > 0 Then
                    Transaccion.Complete()
                    Return True
                Else
                    Transaccion.Complete()
                    Return False
                End If

            Catch ex As SqlException
                Throw ex
            Catch ex As Exception 
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Function
#End Region

#Region "Procedimientos"
    Public Sub InsertarSerial(ByVal Serial As SerialObj)
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaInsertarSerial")
                _database.AddInParameter(Comando, "Encriptado", DbType.String, Serial.Encriptado)
                _database.AddInParameter(Comando, "FechaIngreso", DbType.String, Serial.FechaIngreso)
                _database.ExecuteNonQuery(Comando)

                Transaccion.Complete()

            Catch ex As SqlException
                Throw New AccesoDatosException(My.Resources.ResMensajes.ErrorAlInsertarSerial, ex)
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Sub

    Public Sub ActualizarSerial(ByVal Serial As SerialObj)
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaActualizarSerial")
                _database.AddInParameter(Comando, "Encriptado", DbType.String, Serial.Encriptado)
                _database.AddInParameter(Comando, "Serial", DbType.String, Serial.Serial)
                _database.AddInParameter(Comando, "Trial", DbType.Boolean, Serial.Trial)
                _database.ExecuteNonQuery(Comando)

                Transaccion.Complete()

            Catch ex As SqlException
                Throw New AccesoDatosException(My.Resources.ResMensajes.ErrorAlActualizarSerial, ex)
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Sub
#End Region

End Class
